package com.cheng.im.gateway.handler;

import com.cheng.im.common.bean.msg.ProtoMsgV1;
import com.cheng.im.common.cocurrent.FutureTaskScheduler;
import com.cheng.im.gateway.processer.ChatMsgProcessor;
import com.cheng.im.gateway.session.LocalSession;
import com.cheng.im.gateway.session.manager.SessionManger;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
@ChannelHandler.Sharable
public class ChatMsgHandler extends ChannelInboundHandlerAdapter {

    @Resource
    ChatMsgProcessor chatMsgProcessor;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //判断消息实例
        if (!(msg instanceof ProtoMsgV1.Message)) {
            super.channelRead(ctx, msg);
            return;
        }

        // 判断消息类型
        ProtoMsgV1.Message pkg = (ProtoMsgV1.Message) msg;
        ProtoMsgV1.HeadType headType = ((ProtoMsgV1.Message) msg).getType();

        if (headType.equals((ProtoMsgV1.HeadType.MESSAGE_REQUEST))) {
            // 单聊
            FutureTaskScheduler.add(() -> {
                // 判断是否登录,如果登录了，则为用户消息
                LocalSession session = LocalSession.getSession(ctx);
                if (null != session && session.isLogin())
                {
                    chatMsgProcessor.actionP2P(session, pkg);
                } else {
                    // 没有登录不允许发消息，消息能走到这边情况很特殊，要么就是缓存未释放、或者刚好下线
                }
            });
        } else if (headType.equals(ProtoMsgV1.HeadType.MESSAGE_GROUP_REQUEST)) {
            // 群聊
            FutureTaskScheduler.add(() -> {
                // 判断是否登录,如果登录了，则为用户消息
                LocalSession session = LocalSession.getSession(ctx);
                if (null != session && session.isLogin())
                {
                    chatMsgProcessor.actionP2G(session, pkg);
                } else {
                    // 同上
                }
            });
        } else if (headType.equals(ProtoMsgV1.HeadType.MESSAGE_RECEIVE_ACK)) {
            // 用户消息接收确认ack
            FutureTaskScheduler.add(() -> {
                // 判断是否登录,如果登录了，则为用户消息
                LocalSession session = LocalSession.getSession(ctx);
                if (null != session && session.isLogin())
                {
                    chatMsgProcessor.actionReceiveAck(session, pkg);
                } else {
                    // 同上
                }
            });
        } else if (headType.equals(ProtoMsgV1.HeadType.MESSAGE_READ)) {
            // 用户消息接收确认ack
            FutureTaskScheduler.add(() -> {
                // 判断是否登录,如果登录了，则为用户消息
                LocalSession session = LocalSession.getSession(ctx);
                if (null != session && session.isLogin())
                {
                    chatMsgProcessor.actionMsgRead(session, pkg);
                } else {
                    // 同上
                }
            });
        }


        super.channelRead(ctx, msg);
    }
}
